package ptumall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ptumall.service.HistoryKeywordService;
import ptumall.utils.ApiResponse;

import java.util.List;


@RestController
@RequestMapping("/api/search/history")
public class HistoryKeywordController {

    @Autowired
    private HistoryKeywordService historyKeywordService;

    // 获取历史搜索
    @GetMapping
    public ApiResponse<List<String>> getHistory(
            @RequestAttribute Long userId,
            @RequestParam(defaultValue = "10") int size) {
        return ApiResponse.success(historyKeywordService.getHistory(userId, size));
    }

    // 删除单个历史记录
    @DeleteMapping("/{keyword}")
    public ApiResponse<Void> deleteHistory(
            @RequestAttribute Long userId,
            @PathVariable String keyword) {
        historyKeywordService.deleteHistory(userId, keyword);
        return ApiResponse.success();
    }

    // 清空历史记录
    @DeleteMapping
    public ApiResponse<Void> clearHistory(@RequestAttribute Long userId) {
        historyKeywordService.clearHistory(userId);
        return ApiResponse.success();
    }
}
